package com.lyouu.dataminer.cruisetour;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import org.apache.log4j.Logger;

import com.lyouu.dataminer.db.DbConstants;
import com.lyouu.dataminer.db.DbLyouuParams;
import com.lyouu.dataminer.factory.DaoFactory;
import com.lyouu.dataminer.factory.MysqlDbFactory;
import com.lyouu.dataminer.util.Constants;
import com.lyouu.dataminer.util.TourMap;
import com.lyouu.dataminer.util.Util;

public class CTDehuaDe {
	// private static final String CURRENT_YEAR = String.valueOf(Calendar
	// .getInstance().get(Calendar.YEAR));
	private Logger logger = Logger.getLogger(CTDehuaDe.class);
	private Connection conn;
	private String currency;
	private ArrayList<String> titleList;
	private ArrayList<String> pageUrlList;
	private ArrayList<String> routeList;
	private ArrayList<String> departCityList;
	private ArrayList<String> scheduleList;
	private ArrayList<String> groupIdList;
	private ArrayList<String> travelDateList;
	private ArrayList<String> priceList;
	private ArrayList<String> statusList;
	private ArrayList<String> orderUrlList;

	public CTDehuaDe() {
		this.titleList = new ArrayList<String>();
		this.pageUrlList = new ArrayList<String>();
		this.routeList = new ArrayList<String>();
		this.departCityList = new ArrayList<String>();
		this.scheduleList = new ArrayList<String>();
		this.groupIdList = new ArrayList<String>();
		this.travelDateList = new ArrayList<String>();
		this.priceList = new ArrayList<String>();
		this.statusList = new ArrayList<String>();
		this.orderUrlList = new ArrayList<String>();
	}

	public void getData() throws SQLException {
		conn = MysqlDbFactory.getConnection(DbConstants.DB_LYOUU_SRC);
		String sql = "SELECT * FROM ct_dehua_de ORDER BY id";
		Statement stmt = conn.createStatement();
		ResultSet rs = stmt.executeQuery(sql);
		while (rs.next()) {
			titleList.add(rs.getString(DbConstants.LYOUU_SRC_BUS_TOUR_TITLE));
			pageUrlList.add(rs
					.getString(DbConstants.LYOUU_SRC_BUS_TOUR_PAGE_URL));
			routeList.add(rs.getString(DbConstants.LYOUU_SRC_BUS_TOUR_ROUTE));
			departCityList.add(rs
					.getString(DbConstants.LYOUU_SRC_BUS_TOUR_DEPART_CITY));
			scheduleList.add(rs
					.getString(DbConstants.LYOUU_SRC_BUS_TOUR_SCHEDULE));
			groupIdList.add(rs
					.getString(DbConstants.LYOUU_SRC_BUS_TOUR_GROUP_ID));
			travelDateList.add(rs
					.getString(DbConstants.LYOUU_SRC_BUS_TOUR_TRAVEL_DATE));
			priceList.add(rs.getString(DbConstants.LYOUU_SRC_BUS_TOUR_PRICE));
			statusList.add(rs.getString(DbConstants.LYOUU_SRC_BUS_TOUR_STATUS));
			orderUrlList.add(rs
					.getString(DbConstants.LYOUU_SRC_BUS_TOUR_ORDER_URL));
		}
	}

	public void printData() {
		OutputStream os = null;
		try {
			os = new FileOutputStream("Z:\\Downloads\\ct_dehua_de.txt");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		final PrintStream ps = new PrintStream(os);

		for (int i = 0; i < titleList.size(); i++) {
			ps.println((i + 1) + ":\t" + titleList.get(i) + "\t"
					+ pageUrlList.get(i));
			ps.println(routeList.get(i));
			ps.println(departCityList.get(i));
			ps.println(scheduleList.get(i));
			ps.println(groupIdList.get(i));
			ps.println(travelDateList.get(i));
			ps.println(priceList.get(i));
			ps.println(statusList.get(i));
			ps.println(orderUrlList.get(i));
			ps.println("\n\n");
		}
		ps.close();
		try {
			os.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void updateDbLyouu() throws SQLException {
		String name, content, departCity, routine, departCountry;

		// bustour_schedule table
		ArrayList<String> btsGroupIdList = new ArrayList<String>();
		ArrayList<String> btsDepartDateList = new ArrayList<String>();
		ArrayList<String> btsEndDateList = new ArrayList<String>();
		ArrayList<String> btsAdultPriceList = new ArrayList<String>();
		ArrayList<String> btsChildPriceList = new ArrayList<String>();
		ArrayList<String> btsGroupPriceList = new ArrayList<String>();
		ArrayList<String> btsStatusList = new ArrayList<String>();
		ArrayList<String> btsBookingList = new ArrayList<String>();

		for (int i = 0; i < titleList.size(); i++) {
			name = parseName(titleList.get(i));
			logger.info("\n" + i + "\t" + name);
			content = parseContent(scheduleList.get(i));
			departCity = parseDepartCity(departCityList.get(i));
			routine = parseRoutine(routeList.get(i));
			departCountry = parseDepartCountry(departCity);
			parseGroupId(groupIdList.get(i), btsGroupIdList);
			this.currency = Constants.CURRENCY_EUR;
			parsePrice(priceList.get(i), btsAdultPriceList, btsChildPriceList,
					btsGroupPriceList);
			parseBooking(orderUrlList.get(i), btsBookingList);
			parseTravelDate(travelDateList.get(i), btsDepartDateList,
					btsEndDateList);
			parseStatus(statusList.get(i), btsStatusList);
			// logger.info(departCountry);
			// update db_lyouu
			CruiseTour.insertCruiseTour(pageUrlList.get(i), Constants.OWNER_DE_DEHUA,
					name, Constants.NO_THUMB, content, departCity, routine,
					this.currency, Util.getMinPrice(btsAdultPriceList),
					Constants.PROVIDER_DE_DEHUA,
					Util.getMinPrice(btsChildPriceList),
					Util.getMinPrice(btsGroupPriceList), departCountry,
					btsDepartDateList, btsEndDateList, "", "");
			CruiseTour.insertCruisetourSchedule(Constants.OWNER_DE_DEHUA,
					name, btsGroupIdList, btsDepartDateList, btsEndDateList,
					this.currency, btsAdultPriceList, btsChildPriceList,
					btsGroupPriceList, btsStatusList, btsBookingList,
					pageUrlList.get(i));
		}
	}

	private String parseName(String str) {
		return str.trim();
	}

	private String parseContent(String str) {
		return str.trim();
	}

	private String parseRoutine(String str) {
		// logger.info(str);
		String pattern = "\\&nbsp;|\\s+|\\r|\n|\u3000|[\\s\\p{Zs}]";
		str = str.replaceAll(pattern, "");
		pattern = "第\\d+天";
		str = str.replaceAll(pattern, "");
		pattern = "\\(\\w+\\)";
		str = str.replaceAll(pattern, "");
		str = str.replace("--", "-");
		// logger.info(str);

		String[] routineAry = str.split(Constants.DELIMITER);
		String[] subRoutineAry;
		ArrayList<String> routineList = new ArrayList<String>();
		int i, j, k;
		String place;
		for (i = 0; i < routineAry.length; i++) {
			subRoutineAry = routineAry[i].trim().split("-");
			for (j = 0; j < subRoutineAry.length; j++) {
				place = subRoutineAry[j].trim();
				for (k = 0; k < routineList.size(); k++) {
					if (place.equalsIgnoreCase(routineList.get(k)))
						break;
				}
				if (k == routineList.size())
					routineList.add(place);
			}
			// if (i > 0) {
			// for (int j = 0; j < subRoutineAry.length; j++) {
			// if (!routineList.get(routineList.size() - 1).equals(
			// subRoutineAry[j].trim())) {
			// routineList.add(subRoutineAry[j].trim());
			// }
			// }
			// } else {
			// for (int j = 0; j < subRoutineAry.length; j++)
			// routineList.add(subRoutineAry[j].trim());
			// }
		}
		String resRoutine = "";
		for (i = 0; i < routineList.size() - 1; i++)
			resRoutine += routineList.get(i) + "-";
		resRoutine += routineList.get(i);
		// logger.info(resRoutine);
		return resRoutine;
	}

	private String parseDepartCity(String str) {
		str = str.trim();

		ArrayList<String> departCityList = TourMap.getInstance().retrieveDepartPlace(str);
		String departCities = "";
		if (!departCityList.isEmpty()) {
			int i = 0;
			for (; i < departCityList.size() - 1; i++)
				departCities += departCityList.get(i) + ", ";
			departCities += departCityList.get(i);
		}
		return departCities;
	}

	private String parseDepartCountry(String str) {
		return "64";
	}

	private void parsePrice(String str, ArrayList<String> adultPriceList,
			ArrayList<String> childPriceList, ArrayList<String> groupPriceList) {
		adultPriceList.clear();
		childPriceList.clear();
		groupPriceList.clear();
		str = str.replace(",", ".");
		String[] priceStrAry = str.split(Constants.DELIMITER);
		for (int i = 0; i < priceStrAry.length; i++) {
			adultPriceList.add(priceStrAry[i].split("€")[1]);
			childPriceList.add(priceStrAry[i].split("€")[2]);
			groupPriceList.add(priceStrAry[i].split("€")[3]);
		}
	}

	private void parseGroupId(String str, ArrayList<String> groupIdList) {
		groupIdList.clear();
		String[] groupIdAry = str.split(Constants.DELIMITER);
		for (int i = 0; i < groupIdAry.length; i++) {
			groupIdList.add(groupIdAry[i].trim());
		}
	}

	private void parseTravelDate(String str, ArrayList<String> departDateList,
			ArrayList<String> endDateList) {
		departDateList.clear();
		endDateList.clear();
		String[] dateStrAry = str.split(Constants.DELIMITER);
		for (int i = 0; i < dateStrAry.length; i++) {
			departDateList.add(dateStrAry[i].split("-")[0].trim());
			endDateList.add(dateStrAry[i].split("-")[1].trim());
		}
	}

	private void parseBooking(String str, ArrayList<String> bookingList) {
		bookingList.clear();
		String[] bookingStrAry = str.split(Constants.DELIMITER);
		for (int i = 0; i < bookingStrAry.length; i++) {
			bookingList.add(bookingStrAry[i].trim());
		}
	}

	private void parseStatus(String str, ArrayList<String> statusList) {
		statusList.clear();
		String[] statusStrAry = str.split(Constants.DELIMITER);
		for (int i = 0; i < statusStrAry.length; i++) {
			statusList.add(statusStrAry[i].trim());
		}
	}

	public ArrayList<String> getTitleList() {
		return titleList;
	}

	public void setTitleList(ArrayList<String> titleList) {
		this.titleList = titleList;
	}

	public ArrayList<String> getPageUrlList() {
		return pageUrlList;
	}

	public void setPageUrlList(ArrayList<String> pageUrlList) {
		this.pageUrlList = pageUrlList;
	}

	public ArrayList<String> getRouteList() {
		return routeList;
	}

	public void setRouteList(ArrayList<String> routeList) {
		this.routeList = routeList;
	}

	public ArrayList<String> getScheduleList() {
		return scheduleList;
	}

	public void setScheduleList(ArrayList<String> scheduleList) {
		this.scheduleList = scheduleList;
	}

	public ArrayList<String> getGroupIdList() {
		return groupIdList;
	}

	public void setGroupIdList(ArrayList<String> groupIdList) {
		this.groupIdList = groupIdList;
	}

	public ArrayList<String> getTravelDateList() {
		return this.travelDateList;
	}

	public void setTravelDateList(ArrayList<String> travelDateList) {
		this.travelDateList = travelDateList;
	}

	public ArrayList<String> getPriceList() {
		return priceList;
	}

	public void setPriceList(ArrayList<String> priceList) {
		this.priceList = priceList;
	}

	public ArrayList<String> getStatusList() {
		return statusList;
	}

	public void setStatusList(ArrayList<String> statusList) {
		this.statusList = statusList;
	}

	public ArrayList<String> getOrderLinkList() {
		return orderUrlList;
	}

	public void setOrderLinkList(ArrayList<String> orderUrlList) {
		this.orderUrlList = orderUrlList;
	}

	public ArrayList<String> getDepartCityList() {
		return departCityList;
	}

	public void setDepartCityList(ArrayList<String> departCityList) {
		this.departCityList = departCityList;
	}

	public static void main(String[] args) {
		CTDehuaDe dehua = new CTDehuaDe();
		try {
			dehua.getData();
			dehua.printData();
			CruiseTour.deleteSubjects(Constants.OWNER_DE_DEHUA);
			dehua.updateDbLyouu();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
